# -*- coding: utf-8 -*-
# @Time   : 2022-06-06 17:51:04
# @Author : 搬砖客

import requests
import json
from utils.process_files_utils.yaml_util import YamlUtil
import pytest
from utils.logging_utils.log_util import write_log


# 通过selenium+图像识别登录，获得token值
@pytest.fixture(scope="session", autouse=True)
def get_token():
    write_log("---------------开始云服务自动化测试----------------------")
    from phm_login_selenium import GetToken
    token = GetToken().get_token()
    YamlUtil.write_extract_yaml({"phm_token": token})


def get_uuid():
    host = YamlUtil().read_config_yaml("url", "url_imotor")
    nginx_url = YamlUtil().read_config_yaml("url", "nginx_url")
    url_uuid = host + nginx_url + "/imotor/oauth/image/captcha"
    print(url_uuid)
    header_uuid = {"Content-Type": "application/json;charset=utf-8"}
    res_uuid = json.loads(requests.get(url=url_uuid, headers=header_uuid).text)
    uuid = res_uuid['data']['uuid']
    return uuid


# @pytest.fixture(scope="session", autouse=True)
def get_pc_token():
    uuid = get_uuid()
    username = YamlUtil().read_config_yaml("api_info", "username")
    password = YamlUtil().read_config_yaml("api_info", "password")
    host = YamlUtil().read_config_yaml("url", "url_imotor")
    nginx_url = YamlUtil().read_config_yaml("url", "nginx_url")
    url_token = host + nginx_url + "/imotor/oauth/token"
    header_token = {"Content-Type": "application/json;charset=utf-8"}
    params = {
        "client_id": "imotor",
        "client_secret": 123,
        "platform": "imotor-member",
        "username": {username},
        "password": {password},
        "code": "V587",
        "uuid": {uuid},
        "grant_type": "password"

    }
    res_token = requests.post(url=url_token, params=params, headers=header_token).json()
    token = res_token['data']['access_token']
    YamlUtil.write_extract_yaml({"phm_token": token})
